package com.hl233.system.util;

import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class DateUtil implements Serializable {

    private static final long serialVersionUID = 1L;

    protected DateUtil() {

    }

    public static final String FMT_YYYY = "yyyy";

    public static final String FMT_Y_M = "yyyy-MM";

    public static final String FMT_Y_M_D = "yyyy-MM-dd";

    public static final String FMTYMD = "yyyyMMdd";

    public static final String FMT_YMDHM = "yyyyMMddHHmm";

    public static final String FMT_H_M = "HH:mm";

    public static final String FMT_Y_M_D_H_M = "yyyy-MM-dd HH:mm";

    public static final String FMT_M_D_H_M = "MM-dd HH:mm";

    public static final String FMT_Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss";

    public static final String FMT_Y_M_D_H_M_S_NEW = "yyyy MM dd HH:mm:ss";

    public static final String FMT_Y_M_D_H_M_S_SSS = "yyyy-MM-dd HH:mm:ss.SSS";

    public static final String FMT_YMDHMS = "yyyyMMddHHmmss";


    public static final String FMTYMDHMSSSS = "yyyyMMddHHmmssSSS";

    public static String now() {
        return format(new Date(), FMT_Y_M_D_H_M_S);
    }
    /**
     * 日期转字符串 出错者抛出异常
     *
     * @param date
     * @param formateType
     * @return
     */
    public static String dateToString(Date date, String formateType) {
        return format(date, formateType);
    }

    /**
     * 长整型转日期格式字符串
     *
     * @param lon
     * @param formateType
     * @return
     */
    public static String longToDateString(Long lon, String formateType) {
        return format(lon, formateType);
    }

    /**
     * 字符串转日期 出错者抛出异常
     *
     * @param dateStr
     * @param formateType
     * @return
     */
    public static Date stringToDate(String dateStr, String formateType) {
        return parse(dateStr, formateType);
    }

    /**
     * 返回预设Format的当前日期字符串
     *
     * @return
     */
    public static String getToday() {
        Date today = new Date();
        return format(today);
    }

    /**
     * 使用预设Format格式化Date成FMT_Y_M_D
     *
     * @param date
     * @return
     */
    public static String format(Date date) {
        return date == null ? "" : format(date, FMT_Y_M_D);
    }

    /**
     * 获取系统时间
     */
    public static Date getSystemTime() {
        Calendar cal = Calendar.getInstance();
        return cal.getTime();
    }

    /**
     * 获取今天的开始时刻。
     */
    public static Date getTodayStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取今天的开始时刻。（小时+分钟）
     */
    public static Date getTodayStartHM() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        return cal.getTime();
    }

    /**
     * 使用预设Format格式化Date成FMT_Y_M_D_H_M_S
     *
     * @param mils
     * @return
     */
    public static String format(long mils) {
        return format(new Date(mils), FMT_Y_M_D_H_M_S);
    }

    /**
     * 使用参数Format格式化Date成字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        return date == null ? "" : new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 使用参数Format格式化Date成字符串
     *
     * @param mils
     * @param pattern
     * @return
     */
    public static String format(long mils, String pattern) {
        return format(new Date(mils), pattern);
    }

    /**
     * 使用预设格式将字符串转为Date
     *
     * @param strDate
     * @return
     * @throws ParseException
     */
    public static Date parse(String strDate) {
        return StringUtils.isEmpty(strDate) ? null : parse(strDate, FMT_Y_M_D);
    }

    /**
     * 使用参数Format将字符串转为Date
     *
     * @param strDate
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date parse(String strDate, String pattern) {
        try {
            return StringUtils.isEmpty(strDate) ? null : new SimpleDateFormat(
                    pattern).parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 在日期上增加数个整月
     *
     * @param date
     * @param n
     * @return
     */
    public static Date addMonth(Date date, int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, n);
        return cal.getTime();
    }

    /**
     * 在日期上增加数个整天
     *
     * @param date
     * @param n
     * @return
     */
    public static Date addDay(Date date, int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, n);
        return cal.getTime();
    }

    public static Date getDayStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取original所在时间的上季度的开始<br>
     * 如:2013-07-01 00:00:00.000
     *
     * @param original 参照时间
     * @return
     */
    public static long getPreviousQuarterStart(long original) {
        // 本季度开始-1000L得到了上季度范围的时间,再次以这个为参照物,取季度开始时间
        return getQuarterStart(getQuarterStart(original) - 1000L);
    }

    /**
     * 获取original所在时间的上季度的结束<br>
     * 如:2013-07-01 00:00:00.000
     *
     * @param original 参照时间
     * @return
     */
    public static long getPreviousQuarterEnd(long original) {
        // 本季度开始-1000L得到了上季度范围的时间,再次以这个为参照物,取季度结束时间
        return getQuarterEnd(getQuarterStart(original) - 1000L);
    }

    /**
     * 获取在original所在的时间点的上个月的第一天
     *
     * @param original
     * @return
     */
    public static Date getPreviousMonthStart(long original) {
        Date firstDateOfCurrMonth = DateUtil.getFirstDateOfMonth(new Date(original));
        return DateUtil.addMonth(firstDateOfCurrMonth, -1);
    }

    /**
     * 获取在original所在的时间点的上个月的最后一天
     *
     * @param original
     * @return
     */
    public static Date getPreviousMonthEnd(long original) {
        Date firstDateOfCurrMonth = DateUtil.getFirstDateOfMonth(new Date(original));
        return DateUtil.addDay(firstDateOfCurrMonth, -1);
    }

    /**
     * 获取时间戳original在当年的季度(1/2/3/4)
     *
     * @param original
     * @return
     */
    public static int getQuarterIndex(long original) {
        int quarterIndex = 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        int thisMonth = calendar.get(Calendar.MONTH) + 1;
        if (thisMonth >= 1 && thisMonth <= 3) {
            // 当前在1季度
            quarterIndex = 1;
        } else if (thisMonth >= 4 && thisMonth <= 6) {
            // 当前在2季度
            quarterIndex = 2;
        } else if (thisMonth >= 7 && thisMonth <= 9) {
            // 当前在3季度
            quarterIndex = 3;
        } else if (thisMonth >= 10 && thisMonth <= 12) {
            // 当前在4季度
            // 下一年1月
            quarterIndex = 4;
        }
        return quarterIndex;
    }

    /**
     * 获取original所在时间的下季度的开始<br>
     * 如:2013-07-01 00:00:00.000
     *
     * @param original 参照时间
     * @return
     */
    public static long getNextQuarterStart(long original) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        int thisMonth = calendar.get(Calendar.MONTH) + 1;
        if (thisMonth >= 1 && thisMonth <= 3) {
            // 当前在1季度
            calendar.set(Calendar.MONTH, 3);
        } else if (thisMonth >= 4 && thisMonth <= 6) {
            // 当前在2季度
            calendar.set(Calendar.MONTH, 6);
        } else if (thisMonth >= 7 && thisMonth <= 9) {
            // 当前在3季度
            calendar.set(Calendar.MONTH, 9);
        } else if (thisMonth >= 10 && thisMonth <= 12) {
            // 当前在4季度
            // 下一年1月
            calendar.add(calendar.YEAR, 1);
            calendar.set(Calendar.MONTH, 0);
        }
        // 设置到1号
        calendar.set(calendar.DATE, 1);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取original这个时所对应的时间的所属季度（1~4）
     *
     * @param original
     * @return 1~4
     */
    public static int getQuarterNumber(long original) {
        int quarter = -1;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        int thisMonth = calendar.get(Calendar.MONTH) + 1;
        if (thisMonth >= 1 && thisMonth <= 3) {
            // 1季度
            quarter = 1;
        } else if (thisMonth >= 4 && thisMonth <= 6) {
            // 2季度
            quarter = 2;
        } else if (thisMonth >= 7 && thisMonth <= 9) {
            // 3季度
            quarter = 3;
        } else if (thisMonth >= 10 && thisMonth <= 12) {
            // 4季度
            quarter = 4;
        }
        return quarter;
    }

    /**
     * 获取original所在季度的开始<br>
     * 如:2013-04-01 00:00:00.000
     *
     * @param original 参照时间
     * @return
     */
    public static long getQuarterStart(long original) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        int thisMonth = calendar.get(Calendar.MONTH) + 1;
        if (thisMonth >= 1 && thisMonth <= 3) {
            calendar.set(Calendar.MONTH, 0);
        } else if (thisMonth >= 4 && thisMonth <= 6) {
            calendar.set(Calendar.MONTH, 3);
        } else if (thisMonth >= 7 && thisMonth <= 9) {
            calendar.set(Calendar.MONTH, 6);
        } else if (thisMonth >= 10 && thisMonth <= 12) {
            calendar.set(Calendar.MONTH, 9);
        }
        // 设置到1号
        calendar.set(calendar.DATE, 1);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取original所在季度的结束
     *
     * @param original 参照时间
     * @return
     */
    public static long getQuarterEnd(long original) {
        return getNextQuarterStart(original) - 1L;
    }

    /**
     * 在日期上增加数个小时
     *
     * @param date
     * @param n
     * @return
     */
    public static Date addHour(Date date, int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, n);
        return cal.getTime();
    }

    /**
     * 在日期上增加数个分钟
     *
     * @param date
     * @param n
     * @return
     */
    public static Date addMinute(Date date, int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, n);
        return cal.getTime();
    }

    /**
     * 得到两个日期时间的差额
     *
     * @param date
     * @param otherDate
     * @return
     */
    public static long betDate(Date date, Date otherDate) {
        return date.getTime() - otherDate.getTime();
    }

    /**
     * 以年，月，日来构造一个日期对象
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static Date makeDate(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(1, year);
        calendar.set(2, month - 1);
        calendar.set(5, day);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    /**
     * 以年，月，日，小时，分钟，秒来构造一个日期对象
     *
     * @param year
     * @param month
     * @param day
     * @param hour
     * @param minute
     * @param second
     * @return
     */
    public static Date makeDate(int year, int month, int day, int hour,
                                int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(1, year);
        calendar.set(2, month - 1);
        calendar.set(5, day);
        calendar.set(11, hour);
        calendar.set(12, minute);
        calendar.set(13, second);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    /**
     * 计算两个时间相差的小时数
     *
     * @param maxDate
     * @param minDate
     * @return
     */
    public static long dateDifference(Date maxDate, Date minDate) {
        long diff = maxDate.getTime() - minDate.getTime();
        long diffHours = diff / (60 * 60 * 1000);
        return diffHours;
    }

    /**
     * 计算两个时间相差的小时数
     *
     * @param maxDate
     * @param minDate
     * @return
     */
    public static long dayDifference(Date maxDate, Date minDate) {
        long diff = maxDate.getTime() - minDate.getTime();
        long diffDays = diff / (60 * 60 * 1000 * 24);
        return diffDays;
    }

    /**
     * 计算两个时间相差的秒数
     *
     * @param maxDate
     * @param minDate
     * @return
     */
    public static long secondDifference(Date maxDate, Date minDate) {
        long diff = maxDate.getTime() - minDate.getTime();
        long diffSec = diff / 1000;
        return diffSec;
    }

    /**
     * 返回指定日期是哪一月
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(2) + 1;
    }

    /**
     * 返回指定日期是哪一年
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 返回指定日期是哪一年
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(5);
    }

    /**
     * 返回指定日期是周几
     *
     * @param date
     * @return
     */
    public static int getWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK);

        if (week == 1)
            return 7;
        else
            return week - 1;

    }

    /**
     * 返回指定日期是几点
     *
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);

    }

    /**
     * 返回指定日期的分钟
     *
     * @param date
     * @return
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);

    }

    /**
     * 获得更定日期所在月的最后一天,可能29,30,31,28等
     *
     * @param date
     * @return
     */
    public static Date getLastDateOfMonth(Date date) {
        int year = getYear(date);
        int month = getMonth(date);
        return addDay(addMonth(makeDate(year, month, 1), 1), -1);

    }

    /**
     * 获得指定日期所在月的的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDateOfMonth(Date date) {
        int year = getYear(date);
        int month = getMonth(date);
        return makeDate(year, month, 1);
    }

    /**
     * 先得到指定日期所在周,再得到当周指定周几的日期,resultWeek用于指定周几,取值范围1-7
     *
     * @param date
     * @param resultWeek
     * @return
     */
    public static Date getDayOfWeek(Date date, int resultWeek) {
        if (resultWeek < 1 || resultWeek > 7)
            throw new IllegalArgumentException("resultWeek must in 1-7");
        int week = getWeek(date);
        return addDay(date, resultWeek - week);
    }

    /**
     * 得到以当前时间为基数的序列<br>
     * yyyyMMddHHmmssS格式的时间
     *
     * @return
     */
    public static Long getTime() {
        Long result = new Long(format(new Date(), "yyyyMMddHHmmssS"));
        return result;
    }

    /**
     * 只把original修改到1970年，其他不变
     *
     * @param original
     * @return
     */
    public static long goto1970(long original) {
        return goYear(original, 1970);
    }

    /**
     * 只把original修改到1970年，其他不变
     *
     * @param original
     * @return
     */
    public static long goYear(long original, int targetYears) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        Integer currentYear = Integer.parseInt(format(new Date(original),
                "yyyy"));
        calendar.add(calendar.YEAR, targetYears - currentYear);
        return calendar.getTimeInMillis();
    }

    public static long getNextDay(long original) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        calendar.add(calendar.DATE, 1);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static long getNextWeek(long original) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        calendar.add(calendar.DATE, 8);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static long getNextTwoWeek(long original) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        calendar.add(calendar.DATE, 15);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static long getNextThreeWeek(long original) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        calendar.add(calendar.DATE, 22);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static long getNextMonth(long original) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        calendar.add(calendar.MONTH, 1);
        calendar.add(calendar.DATE, 1);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static long getNextTwoMonth(long original) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        calendar.add(calendar.MONTH, 2);
        calendar.add(calendar.DATE, 1);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 把当前时间移到三个月后：如当前2013-5-27-->2013-8-27
     *
     * @param original
     * @return
     */
    public static long getNextThreeMonth(long original) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        calendar.add(calendar.MONTH, 3);
        calendar.add(calendar.DATE, 1);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static long getHalfYear(long original) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        calendar.add(calendar.MONTH, 6);
        calendar.add(calendar.DATE, 1);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static long getNextYear(long original) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(original);
        calendar.add(calendar.YEAR, 1);
        calendar.add(calendar.DATE, 1);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static Date addDays(Date date, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, amount);
        return c.getTime();
    }

    public static Date addMonths(Date date, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, amount);
        return c.getTime();
    }

    public static String formatDay(Date date) {
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(date);
    }

    /**
     * 获取本周的周一日期
     *
     * @return date
     */
    public static Date getMondayThisWeek() {
        Date todayStart = getTodayStartTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(todayStart);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return calendar.getTime();
    }

    /**
     * 获取上周周一的日期
     *
     * @return date
     */
    public static Date getPreMonday() {
        Date thisMonday = getMondayThisWeek();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(thisMonday);
        calendar.add(Calendar.DATE, -7);
        return calendar.getTime();
    }

    /**
     * 获取指定日期是星期几
     * 参数为null时表示获取当前日期是星期几
     *
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekOfDays[w];
    }

    /**
     * 拼时间字符串 根据秒数换算成字符串 xx天xx小时xx秒
     *
     * @param howlong 总时长
     * @return
     */
    public static String stos(Integer howlong) {
        String timeLength = "";
        int daymm = 86400;
        int hourmm = 3600;
        int minmm = 60;
        int day = 0;
        int hour = 0;
        int min = 0;
        int second = 0;
        //大于天数的处理
        if (howlong >= daymm) {
            day = (int) (Math.floor(howlong / daymm));
            //大于小时的处理
            if (howlong % daymm >= hourmm) {
                hour = (int) (Math.floor(howlong % daymm) / hourmm);
                if ((howlong % daymm) % hourmm >= minmm) {
                    min = (int) (Math.floor(howlong % daymm) % hourmm / minmm);
                    second = (howlong % daymm) % hourmm % minmm;
                }
                //不足小时的处理
            } else {
                //大于分钟的处理
                if (howlong % daymm >= minmm) {
                    min = (int) (Math.floor(howlong % daymm) / minmm);
                    second = (howlong % daymm) % minmm;
                } else {
                    //秒数
                    second = howlong;
                }
            }
            //不足天数的的处理
        } else {
            //大于小时的处理
            if (howlong >= hourmm) {
                hour = (int) (Math.floor(howlong / hourmm));
                if (howlong % hourmm >= minmm) {
                    min = (int) (Math.floor(howlong % hourmm) / minmm);
                    second = (howlong % hourmm) % minmm;
                }
                //不足小时的处理
            } else {
                //大于分钟的处理
                if (howlong >= minmm) {
                    min = (int) (Math.floor(howlong / minmm));
                    second = (howlong % minmm);
                } else {
                    //秒数
                    second = howlong;
                }
            }
        }
        if (day > 0) {
            timeLength += day + "天";
        }
        if (hour > 0) {
            timeLength += hour + "小时";
        }
        if (min > 0) {
            timeLength += min + "分";
        }
//        if (timeLength.length() > 0) {
//            if (second > 0) {
//                timeLength += second + "秒";
//            }
//        } else {
//            timeLength = second + "秒";
//        }
        return timeLength;
    }

    /**
     * 根据开始时间和结束时间返回时间段内的时间集合
     *
     * @param beginDate
     * @param endDate
     * @return List
     */
    public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
        List<Date> lDate = new ArrayList<Date>();
        lDate.add(beginDate);// 把开始时间加入集合
        Calendar cal = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(beginDate);
        boolean bContinue = true;
        while (bContinue) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // 测试此日期是否在指定日期之后
            if (endDate.after(cal.getTime())) {
                lDate.add(cal.getTime());
            } else {
                break;
            }
        }
        lDate.add(endDate);// 把结束时间加入集合
        return lDate;
    }

    /**
     * 获取当前时间 前15 的日期  和当前时间 比如当前是2021-02-26 获取到 开始时间为2021-02-26  结束时间为2021-02-26
     *
     * @return
     */
    public static String getFirstHalfMonth() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = df.format(new Date().getTime() - 14 * 24 * 60 * 60 * 1000);
        String endTime = df.format(new Date());
        return startTime + ";" + endTime;
    }

    public static String formatDateStr(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date parse = format.parse(dateStr);
            return format1.format(parse);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static int getNowYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.YEAR);
    }

    public static void main(String[] arg) {
//        formatDateStr("2021-11-10 13:00:00.0");

        int nowYear = getNowYear();
    }


}
